package com.nanhai.buddhist.college.module.system.service.rolegroup;

import com.nanhai.buddhist.college.framework.common.util.collection.CollectionUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.nanhai.buddhist.college.module.system.controller.admin.rolegroup.vo.*;
import com.nanhai.buddhist.college.module.system.dal.dataobject.rolegroup.RoleGroupUserDO;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;

import com.nanhai.buddhist.college.module.system.convert.rolegroup.RoleGroupUserConvert;
import com.nanhai.buddhist.college.module.system.dal.mysql.rolegroup.RoleGroupUserMapper;

import static com.nanhai.buddhist.college.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.nanhai.buddhist.college.module.system.enums.ErrorCodeConstants.*;

/**
 * 角色群组用户 Service 实现类
 *
 * @author xqenzo@163.com
 */
@Service
@Validated
public class RoleGroupUserServiceImpl implements RoleGroupUserService {

    @Resource
    private RoleGroupUserMapper roleGroupUserMapper;

    @Override
    public Long createRoleGroupUser(RoleGroupUserCreateReqVO createReqVO) {
        // 插入
        RoleGroupUserDO roleGroupUser = RoleGroupUserConvert.INSTANCE.convert(createReqVO);
        roleGroupUserMapper.insert(roleGroupUser);
        // 返回
        return roleGroupUser.getId();
    }

    @Override
    public void updateRoleGroupUser(RoleGroupUserUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateRoleGroupUserExists(updateReqVO.getId());
        // 更新
        RoleGroupUserDO updateObj = RoleGroupUserConvert.INSTANCE.convert(updateReqVO);
        roleGroupUserMapper.updateById(updateObj);
    }

    @Override
    public void deleteRoleGroupUser(Long id) {
        // 校验存在
        this.validateRoleGroupUserExists(id);
        // 删除
        roleGroupUserMapper.deleteById(id);
    }

    private void validateRoleGroupUserExists(Long id) {
        if (roleGroupUserMapper.selectById(id) == null) {
            throw exception(ROLE_GROUP_USER_NOT_EXISTS);
        }
    }

    @Override
    public RoleGroupUserDO getRoleGroupUser(Long id) {
        return roleGroupUserMapper.selectById(id);
    }

    @Override
    public List<RoleGroupUserDO> getRoleGroupUserList(Collection<Long> ids) {
        return CollectionUtils.isEmpty(ids) ? Collections.emptyList() : roleGroupUserMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<RoleGroupUserDO> getRoleGroupUserPage(RoleGroupUserPageReqVO pageReqVO) {
        return roleGroupUserMapper.selectPage(pageReqVO);
    }

    @Override
    public List<RoleGroupUserDO> getRoleGroupUserList(RoleGroupUserExportReqVO exportReqVO) {
        return roleGroupUserMapper.selectList(exportReqVO);
    }



    /**
    * 获得角色群组用户列表, 根据 systemRoleGroupId 查询
    *
    * @param  systemRoleGroupId  群组名称 查询条件
    * @return 角色群组用户列表
    */
    @Override
    public List<RoleGroupUserDO> getRoleGroupUserListBysystemRoleGroupId(Long systemRoleGroupId){
        return roleGroupUserMapper.selectList(RoleGroupUserDO::getSystemRoleGroupId,systemRoleGroupId);
    }

    /**
    * 获得角色群组用户列表, 根据 userId 查询
    *
    * @param  userId  用户ID 查询条件
    * @return 角色群组用户列表
    */
    @Override
    public List<RoleGroupUserDO> getRoleGroupUserListByuserId(String userId){
        return roleGroupUserMapper.selectList(RoleGroupUserDO::getUserId,userId);
    }

}
